import CreateRoleTransfer from '../components/CreateRoleTransfer'
import CreateOrgTransfer from '../components/CreateOrgTransfer'
import CreateMsgRoleTransfer from "@/pages/System/RoleAssignment/components/CreateMsgRoleTransfer";
import React, { useState, useRef, useEffect } from 'react'
import { PageHeaderWrapper } from '@ant-design/pro-layout'
import ProTable, { ProColumns, ActionType } from '@ant-design/pro-table'
import { connect, history } from 'umi'
import { message, Divider } from 'antd'
import { getUserList, updateUserRole, updateUserGroup, updateUserMsgRole } from '../service'

const TableList = (props) => {
  const actionRef = useRef();
  const [done, setDone] = useState(false);
  const [visible, setVisible] = useState(null);
  const [orgTransferVisible, setOrgTransferVisible] = useState(null);
  const [msgRoleTransferVisible, setMsgRoleTransferVisible] = useState(null);
  const [current, setCurrent] = useState(undefined);

  const showUserRole = (userInfo) => {
    setVisible(true);
    setCurrent(userInfo);
  };

  const showUserGroup = (userInfo) => {
    setOrgTransferVisible(true);
    setCurrent(userInfo);
  };

  const showMsgRole = (userInfo) => {
    setMsgRoleTransferVisible(true);
    setCurrent(userInfo);
  };

  const handleCancel = () => {
    setCurrent(null);
    setVisible(false);
    setOrgTransferVisible(false);
    setMsgRoleTransferVisible(false);
  };

  const handleSubmit = (values) => {
    let {flag, ...data} = values;
    setDone(true);
    setCurrent(null);
    if (flag === 'updateOrgGroup'){
      setOrgTransferVisible(false);
      updateUserGroup(data).then(r => {
        if (r.flag){
          message.success(`用户公司组分配成功。`);
        }else {
          message.error(`用户角色分配失败。`);
        }
      })
    }else if (flag === 'updateUserRole'){
      setVisible(false);
      updateUserRole(data).then(r => {
        if (r.flag){
          message.success(`用户角色分配成功。`);
        }else {
          message.error(`用户角色分配失败。`);
        }
      })
    }else if (flag === 'updateUserMsgRole'){
      setMsgRoleTransferVisible(false);
      updateUserMsgRole(data).then(r => {
        if (r.flag){
          message.success(`用户消息角色分配成功。`);
        }else {
          message.error(`用户消息角色分配失败。`);
        }
      })
    }
  };

  const columns = [
    {
      title: '序号',
      render:(text,record,index)=>`${index+1}`,
      hideInSearch: true,
      align: 'center',
    },
    {
      title: '用户名',
      dataIndex: 'name',
      align: 'center',
    },
    {
      title: '所属公司',
      dataIndex: 'orgname',
      hideInSearch: true,
      align: 'center',
    },
    {
      title: '所属部门',
      dataIndex: 'department',
      hideInSearch: true,
      align: 'center',
    },
    {
      title: '岗位',
      dataIndex: 'jobname',
      hideInSearch: true,
      align: 'center',
    },
    {
      title: '邮件地址',
      dataIndex: 'email',
      hideInSearch: true,
      align: 'center',
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      align: 'center',
      render: (item, record) => (
        <>
          {<a onClick={() => showUserRole(record)}>分配角色</a>}
          {<Divider type="vertical" />}
          {<a onClick={() => showUserGroup(record)}>分配公司组</a>}
          {<Divider type="vertical" />}
          {<a onClick={() => showMsgRole(record)}>分配消息角色</a>}
        </>
      ),
    },
  ];


  return (
    <PageHeaderWrapper>
      <ProTable
        actionRef={actionRef}
        rowKey="userId"
        request={(params) => getUserList(params)}
        columns={columns}
        options={false}
      />
      <CreateRoleTransfer
        done={done}
        current={current}
        visible={visible}
        onCancel={handleCancel}
        onSubmit={handleSubmit}
      >
      </CreateRoleTransfer>
      <CreateOrgTransfer
        done={done}
        current={current}
        visible={orgTransferVisible}
        onCancel={handleCancel}
        onSubmit={handleSubmit}
      >
      </CreateOrgTransfer>
      <CreateMsgRoleTransfer
        done={done}
        current={current}
        visible={msgRoleTransferVisible}
        onCancel={handleCancel}
        onSubmit={handleSubmit}
      >
      </CreateMsgRoleTransfer>
    </PageHeaderWrapper>
  )
};

export default connect(({  }) => ({
}))(TableList)
